$(function () {
    $("#inpay_datagrid").datagrid({
        fit: true,
        fitColumns: true,
        toolbar: '#inpay_toolbar',
        url: '/inpay/list.do',
        singleSelect: true,
        striped: true,
        rownumbers: true,
        pagination: true,
        columns: [[
            {
                title: '保单号', field: 'outpay' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return  value ? value.guard.num : "";
                }
            },
            {
                title: '投保人', field: 'outpay2' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return row.outpay ? row.outpay.client.name : "";
                }
            },
            {
                title: '电话', field: 'outpay3' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return row.outpay ? row.outpay.client.tel: "";
                }
            },
            {
                title: '客户地址', field: 'outpay4' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return row.outpay ? row.outpay.client.address : "";
                }
            },
            {
                title: '身份证', field: 'outpay5' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return row.outpay ? row.outpay.client.card : "";
                }
            },
            {
                title: '核保单号', field: 'outpay6' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return row.outpay ? row.outpay.guard.num : " ";
                }
            },
            {
                title: '保费金额', field: 'outpay7' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return row.outpay ? row.outpay.guard.product.cost : "";
                }
            },
            {
                title: '缴费状态', field: 'outpay8' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return row.outpay.guard ? "<font color='green'>已缴费</font>" : "";
                }
            },
            {title: '缴费方式', field: 'outpay9' ,align:'center', width: 30,
                formatter: function(value,row,index){
                    if (value = 0){
                        return '<font>现金支付</font>';
                    }else if(value = 1){
                        return '<font>刷卡支付</font>';
                    }else if(value = 2){
                        return '<font>支票支付</font>';
                    }else{
                        return '';
                    }
                }
            },
            {
                title: '起保日期', field: 'outpay10' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return row.outpay ? row.outpay.beginDate: "";
                }
            },
            {
                title: '止保日期', field: 'outpay11' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return row.outpay ? row.outpay.endDate: "";
                }
            },
            {
                title: '保险机构', field: 'outpay12' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return row.outpay.promissgroup.promise ? row.outpay.promissgroup.promise.name : "";
                }
            },
            {
                title: '录入人', field: 'outpay13' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return row.outpay.guard.employee ? row.outpay.guard.employee.realname : "";
                }
            }
        ]]
    })

    $("#inpay_dialog").dialog({
        width: 360,
        height: 450,
        buttons: '#inpay_btns',
        border:'thin',
        closed: true,
        onClose: function () {
            $("#inpay_form").form("clear")
        }
    })

    //方法点击事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd](this);
    })

    var methodObj = {

        //高级查询
        doSearch:function () {
            var keyword = $("#keyword").textbox("getValue");
            var beginDate = $("#beginDate").textbox("getValue");
            var endDate = $("#endDate").textbox("getValue");
            var num = $("#num").textbox("getValue");

            $("#inpay_datagrid").datagrid("load",{
                keyword:keyword,
                num:num,
                endDate:endDate,
                beginDate:beginDate
            })
        },
        //编辑按钮
        edit: function () {
            var data = $("#inpay_datagrid").datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            //各种对象回显
            if (data.outpay.client) {
                data['outpay.client.name'] = data.outpay.client.name;
            }
            if (data.outpay.client) {
                data['outpay.client.card'] = data.outpay.client.card;
            }
            if (data.outpay.guard) {
                data['outpay.guard.num'] = data.outpay.guard.num;
            }
            if (data.outpay.beginDate) {
                data['outpay.beginDate'] = data.outpay.beginDate;
            }
            if (data.outpay.endDate) {
                data['outpay.endDate'] = data.outpay.endDate;
            }
            if (data.outpay.guard) {
                data['outpay.guard.product.cost'] = data.outpay.guard.product.cost;
            }
            if (data.outpay.promissgroup.promise) {
                data['outpay.promissgroup.promise.name'] = data.outpay.promissgroup.promise.name;
            }
            if (data.outpay.guard.employee) {
                data['outpay.guard.employee.realname'] = data.outpay.guard.employee.realname;
            }
            if (data.outpay.method) {
                data['outpay.method'] = data.outpay.method;
            }
            $('#product_datagrid').datagrid({
                url:"/product/selectByPrimaryKey.do?id="+data.outpay.product.id,
                fitColumns: true,
                singleSelect: true,
                striped: true,
                scrollbarSize:0,
                columns:[[
                    {title: '险种', field: 'name' ,align:'center', width:10},
                    {title: '险种金额', field: 'cost' ,align:'center', width: 10}
                ]]
            });
            $("#inpay_form").form('load', data);

            $("#inpay_dialog").dialog("open");
            $("#inpay_dialog").dialog('setTitle','保单信息');
        },
        //刷新按钮
        reload: function () {
            $("#inpay_datagrid").datagrid("reload");
        },
        //取消按钮
        cancel: function () {
            $("#inpay_dialog").dialog("close");
        }
    }
})

